package net.alan.modules.repository;

import net.alan.modules.bean.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;

/**
 * Author 李岩飞
 * Email eliyanfei@126.com
 * 2017/11/22
 */
public interface UserRepository extends JpaRepository<User, Long> {
    /**
     * 根据账号查询
     *
     * @param account
     * @return
     */
    User findByAccount(String account);

    /**
     * 根据邮箱查询
     *
     * @param email
     * @return
     */
    User findByEmail(String email);

    /**
     * 根据邮箱查询
     *
     * @param name
     * @return
     */
    User findByName(String name);

    /**
     * 通过名称或者账号搜索用户
     *
     * @param account
     * @param name
     * @param pageable
     * @return
     */
    Page<User> findByAccountOrName(String account, String name, Pageable pageable);

    @Modifying
    @Transactional
    @Query(value = "select count(*) from user where last_login >= ?1 ", nativeQuery = true)
    List<BigInteger> getRegistersByLastLogin(Date date);

    @Modifying
    @Transactional
    @Query(value = "select count(*) from user where created >= ?1 ", nativeQuery = true)
    List<BigInteger> getLoginsByCreated(Date date);
}
